//
//  HBTextViewAlter.m
//  HeartBeat
//
//  Created by karong on 2023/9/5.
//

#import "HBTextViewAlter.h"

@interface HBTextViewAlter ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (weak, nonatomic) IBOutlet YYTextView *tv;
@property (weak, nonatomic) IBOutlet UILabel *textLenthLabel;

@end

@implementation HBTextViewAlter

- (void)awakeFromNib {
    [super awakeFromNib];
    [self resetTextLengthWithLength:0];
    self.tv.placeholderFont = [UIFont systemFontOfSize:15 weight:(UIFontWeightRegular)];
    self.tv.placeholderTextColor = [UIColor colorFromHexRGB:@"828282"];
    self.tv.placeholderText = WSLocalized(@"點擊添加個人簡介");
    self.tv.tintColor = UIColor.font_yellow_FFCC00;
}

- (void)layoutView {
    [super layoutView];
    self.tv.text = self.contentStr;
    [self resetTextLengthWithLength:self.contentStr.length];
}

- (IBAction)cancelAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.cancelHandle) {
            self.cancelHandle();
        }
    }];
}

- (IBAction)confirmAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.confirmHandle) {
            self.confirmHandle(self.tv.text);
        }
    }];
}

#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@""]) {
        [self resetTextLengthWithLength:textView.text.length - 1];
        return YES;
    }
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if (range.length == 1 && text.length == 0) {
        [self resetTextLengthWithLength:textView.text.length + text.length];
        return YES;
    }
    else if (textView.text.length + text.length > self.maxWordCount) {
        NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
        textView.text = [newText substringToIndex:MIN(newText.length, self.maxWordCount)];
        [self resetTextLengthWithLength:textView.text.length];
        return NO;
    }
    [self resetTextLengthWithLength:textView.text.length + text.length];
    return YES;
}

- (void)resetTextLengthWithLength:(NSInteger)length {
    NSString * str = SF(@"%ld/%ld",(long)length, (long)self.maxWordCount);
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:str
                                                                                 attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12], NSForegroundColorAttributeName: UIColor.font_black_1A1A1A}];
//    NSRange range = NSMakeRange(str.length - 3, 3);
//    [attrText addAttributes:@{NSForegroundColorAttributeName: [UIColor colorFromHexRGB:@"e0e0e0"]} range:range];
    self.textLenthLabel.attributedText = attrText;
}

- (void)setContentStr:(NSString *)contentStr {
    [super setContentStr:contentStr];
    self.tv.text = contentStr;
    [self resetTextLengthWithLength:contentStr.length];
}

@end
